package com.wangyadong.hobby.excel;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class LocalDateTimeUtil {

    public static void main(String[] args) {


        System.out.println(LocalDateTime.now().toString());//2018-03-27T13:55:34.047
        System.out.println(LocalDateTime.now().toLocalDate().toString());//2018-03-27
        System.out.println(LocalDateTime.now().toLocalTime().toString());//13:55:34.047

        System.out.println(LocalDateTime.MAX.toString());//+999999999-12-31T23:59:59.999999999
        System.out.println(LocalDateTime.MIN.toString());//-999999999-01-01T00:00

        LocalDateTime dateTime = LocalDateTime.of(2019, 05, 05, 05, 05);
        String time = new LocalDateTimeUtil().localDateTimeToString(dateTime, "yyyy-MM-dd'T'HH:mm:ss'Z'");
        System.out.println(time);
        //	DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");


    }


    /**
     * Date转换为LocalDateTime
     *
     * @param date
     */
    public static LocalDateTime date2LocalDateTime(Date date) {
        Instant instant = date.toInstant();//An instantaneous point on the time-line.(时间线上的一个瞬时点。)
        ZoneId zoneId = ZoneId.systemDefault();//A time-zone ID, such as {@code Europe/Paris}.(时区)
        return instant.atZone(zoneId).toLocalDateTime();

//        System.out.println(localDateTime.toString());//2018-03-27T14:07:32.668
//        System.out.println(localDateTime.toLocalDate() + " " + localDateTime.toLocalTime());//2018-03-27 14:48:57.453
//

    }

    public static String localDateTimeToString(LocalDateTime localDateTime) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//This class is immutable and thread-safe.@since 1.8
        return dateTimeFormatter.format(localDateTime);//2018-03-27 14:52:57
    }

    public static String localDateTimeToString(LocalDateTime localDateTime, String paent) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(paent);//This class is immutable and thread-safe.@since 1.8
        return dateTimeFormatter.format(localDateTime);//2018-03-27 14:52:57
    }

    /**
     * LocalDateTime转换为Date
     *
     * @param localDateTime
     */
    public static Date localDateTime2Date(LocalDateTime localDateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);//Combines this date-time with a time-zone to create a  ZonedDateTime.
        return Date.from(zdt.toInstant());
//        System.out.println(date.toString());//Tue Mar 27 14:17:17 CST 2018
    }

}
